package com.itcs.box.server.conf;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import com.itcs.box.BoxConstants;
import com.itcs.box.server.resolver.AppTopicBoxResolver;
import com.itcs.box.server.resolver.BoxIdCheckTopicResolver;

/**
 * 发布订阅消息监听
 * 
 * @author LiuPeng
 * @date 2018年1月26日
 * 
 */
@Component
public class TopicMsgReceiver implements MessageListener {

	private Log logger = LogFactory.getLog(getClass());

	@Autowired
	private AppTopicBoxResolver appTopicBoxResolver;

	@Autowired
	private BoxIdCheckTopicResolver boxIdCheckTopicResolver;

	@Override
	public void onMessage(Message message, byte[] pattern) {
		String channelName = new String(pattern);
		if (BoxConstants.PUBSUB_BOX_CHANNEL.equals(channelName)) {
			appTopicBoxResolver.subBoxMsg(message.toString());
		} else if (BoxConstants.PUBSUB_BOXID_CHECK.equals(channelName)) {
			boxIdCheckTopicResolver.check(message.toString());
		} else {
			logger.warn("未知广播类型:" + channelName + "-" + message.toString());
		}
	}

}
